<template>
    <div>
        <Modal :title="modalTitle" v-model="modalShow" :width="modalWidth" :mask-closable="false" :closable="false">
            <div v-show="modalType === 'edit' || modalType === 'add'">
                <Form ref="formItem" :rules="ruleValidate" label-position="right" :model="formItem" :label-width="130">
                    <FormItem label="姓名：" prop="name">
                        <Input type="text" maxlength="20" v-model="formItem.name" placeholder="请输入姓名" clearable></Input>
                    </FormItem>
                    <FormItem label="电话：" prop="mobile">
                        <Input type="text" maxlength="20" v-model="formItem.mobile" placeholder="请输入电话" clearable></Input>
                    </FormItem>
                    <FormItem label="身份证号：" prop="card_no">
                        <Input type="text" maxlength="20" v-model="formItem.card_no" placeholder="请输入身份证号" clearable></Input>
                    </FormItem>
                </Form>
            </div>
            <div v-show="modalType === 'info'">
                <Form label-position="right" :model="formItem" :label-width="130">
                    <Row type="flex" justify="space-between">
                        <Col span="8">
                            <FormItem label="身份证号：">
                                {{formItem.card_no? formItem.card_no : '无'}}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem label="联系人姓名：">
                                {{formItem.name? formItem.name : '无'}}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem label="联系电话：">
                                {{formItem.mobile? formItem.mobile : '无'}}
                            </FormItem>
                        </Col>
                    </Row>
                </Form>
            </div>
            <div slot="footer">
                <template v-if="modalType === 'edit' || modalType === 'add'">
                    <Button type="text" @click="cancelOption">取消</Button>
                    <Button type="success" @click="saveOption" :loading="btnLoading">保存</Button>
                </template>
                <template v-if="modalType === 'info'">
                    <Button type="text" @click="cancelOption">关闭</Button>
                </template>
            </div>
        </Modal>
    </div>
</template>

<script>
import {
  updateHandleInfo
} from '@/api/pass'
import { saveExcelData } from '@/api/worker'
// import { formatDate } from '@/components/dateFormat'
import { identityCodeValid } from '@/libs/tools'

const validateCardno = (rule, value, callback) => {
  const rtn = identityCodeValid(value)
  if (!rtn.pass) {
    callback(new Error(rtn.tip))
  } else {
    callback()
  }
}

export default {
  name: 'optionModal',
  props: {
    modalTitle: {
      type: String,
      default: () => {
        return ''
      }
    },
    modalType: {
      type: String,
      default: () => {
        return 'info'
      }
    },
    modalShow: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    modalWidth: {
      type: Number,
      default: () => {
        return 800
      }
    },
    dataObj: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      ruleValidate: {
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        card_no: [
          { required: true, validator: validateCardno, trigger: 'blur' }
        ]
        // mobile: [
        //   { required: true, message: '电话不能为空', trigger: 'blur' }
        // ]
      },
      tableColumns: [
        {
          key: 'name',
          title: '姓名',
          // width: 70,
          // fixed: 'left',
          align: 'center'
        },
        {
          title: '身份证号',
          key: 'card_no',
          tooltip: true,
          // width: 200,
          align: 'center'
        },
        {
          title: '手机号',
          key: 'mobile',
          tooltip: true,
          // width: 200,
          align: 'center'
        },
        {
          title: '操作',
          // minWidth: 350,
          // fixed: 'right',
          slot: 'action',
          align: 'center'
        }
      ],
      tableData: [],
      orignalData: [],
      btnLoading: false,
      formItem: {},
      categoryObj: {},
      tableLoading: false
    }
  },
  methods: {
    cancelOption () {
      this.$emit('cancelOption')
    },
    saveOption () {
      let param = {}
      if (this.modalType === 'add') {
        param = this.formItem
        this.$delete(param, 'id')
      } else if (this.modalType === 'edit') {
        param = this.formItem
      }
      // 数据校验
      this.$refs['formItem'].validate((valid) => {
        if (valid) {
          // this.formItem.publish_time = formatDate(this.formItem.publish_time, 'yyyy-MM-dd')
          // console.log('提交表单', this.formItem)
          if (this.modalType === 'add') {
            this.addDataAction(param)
          } else if (this.modalType === 'edit') {
            this.updateDataAction(param)
          }
        } else {
          this.$Message.error({
            content: '操作失败,请核对添加信息后重试'
          })
        }
      })
    },
    updateDataAction (param) {
      this.btnLoading = true
      updateHandleInfo(param).then(ret => {
        let data = ret.data
        if (data.errcode === 0) {
          this.btnLoading = false
          this.success()
          this.$emit('saveOption')
        } else {
          this.btnLoading = false
          this.error(data)
        }
      })
    },
    addDataAction (val) {
      this.btnLoading = true
      let arr = []
      const obj = this.$common.xeUtils.clone(val)
      arr.push(obj)
      let param = { type: 'handle', dataList: arr }
      saveExcelData(param).then(ret => {
        let data = ret.data
        if (data.errcode === 0) {
          this.btnLoading = false
          if (data.data.falseList && data.data.falseList.length > 0) {
            this.$Message.error('此身份证号已重复')
          } else {
            this.success()
            this.$emit('saveOption')
          }
        } else {
          this.btnLoading = false
          this.error(data)
        }
      })
    },
    success (val) {
      val = this.$common.xeUtils.isEmpty(val) ? '操作成功' : val
      this.$Message.success(val)
    },
    error (data) {
      this.$Message.error({
        content: '操作失败,\n' + data.errmsg
      })
    }
  },
  mounted () {
  },
  watch: {
    dataObj (val) {
      this.formItem = val
    }
  },
  computed: {}
}
</script>

<style scoped>
    .badge{
        width: 100px;
        height: 100px;
        background: #eee;
        border-radius: 6px;
        display: inline-block;
    }

    .action-btn {
        margin-right: 5px;
        margin-bottom: 5px;
    }
</style>
